//
//  AlbumTableViewCell.m
//  Everest
//
//  Created by 刘宇文 on 14-12-23.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "AlbumTableViewCell.h"
#import "AlbumTracks.h"
#import "UIImageView+WebCache.h"
#import "MusicSearch.h"

#define kCellWidth [UIApplication sharedApplication].keyWindow.bounds.size.width

@interface AlbumTableViewCell ()

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIView *twoView;
@property (nonatomic, retain) UIView *byView;
@property (nonatomic, retain) UILabel *byLabel;
@property (nonatomic, retain) UILabel *nameLabel;

@property (nonatomic, retain) UIView *mView;
@property (nonatomic, retain) UILabel *sizeLabel;
@property (nonatomic, retain) UILabel *mLabel;

@property (nonatomic, retain) UIImageView *playImageView;
@property (nonatomic, retain) UIImageView *heartImageView;
@property (nonatomic, retain) UIImageView *buildTimeImageView;
@property (nonatomic, retain) UILabel *playLabel;
@property (nonatomic, retain) UILabel *heartLabel;
@property (nonatomic, retain) UILabel *buildTimeLabel;
//@property (nonatomic, retain) UIView *linkView;

@property (nonatomic, retain) UIView *fiveView;
@property (nonatomic, retain) UIView *playView;
@property (nonatomic, retain) UIView *heartView;
@property (nonatomic, retain) UIView *buildTimeView;

@property (nonatomic, retain) UIImageView *bigImageView;

@property (nonatomic, retain) UIButton *playButton;

@end

@implementation AlbumTableViewCell


- (void)dealloc
{
    [_titleLabel release];
    [_twoView release];
    [_byView release];
    [_byLabel release];
    [_nameLabel release];
    [_mView release];
    [_sizeLabel release];
    [_mLabel release];
    [_playImageView release];
    [_heartImageView release];
    [_buildTimeImageView release];
    [_playLabel release];
    [_heartLabel release];
    [_buildTimeLabel release];
    [_playView release];
    [_heartView release];
    [_buildTimeView release];
    [_fiveView release];
    [_bigImageView release];
    [_playButton release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.fiveView = [[UIView alloc] init];
        [self.contentView addSubview:self.fiveView];
        [self.fiveView release];
        
        self.playView = [[UIView alloc] init];
        [self.fiveView addSubview:self.playView];
        [self.playView release];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.playView addSubview:self.playImageView];
        [self.playImageView release];
        
        self.playLabel = [[UILabel alloc] init];
        [self.playView addSubview:self.playLabel];
        [self.playLabel release];
        
        self.heartView = [[UIView alloc] init];
        [self.fiveView addSubview:self.heartView];
        [self.heartView release];
        
        self.heartImageView = [[UIImageView alloc] init];
        [self.heartView addSubview:self.heartImageView];
        [self.heartImageView release];
        
        self.heartLabel = [[UILabel alloc] init];
        [self.heartView addSubview:self.heartLabel];
        [self.heartLabel release];
        
        self.buildTimeView = [[UIView alloc] init];
        [self.fiveView addSubview:self.buildTimeView];
        [self.buildTimeView release];
        
        self.buildTimeImageView = [[UIImageView alloc] init];
        [self.buildTimeView addSubview:self.buildTimeImageView];
        [self.buildTimeImageView release];
        
        self.buildTimeLabel = [[UILabel alloc] init];
        [self.buildTimeView addSubview:self.buildTimeLabel];
        [self.buildTimeLabel release];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [self.titleLabel release];
        
        self.twoView = [[UIView alloc] init];
        [self.contentView addSubview:self.twoView];
        [self.twoView release];
        
        self.byView = [[UIView alloc] init];
        [self.twoView addSubview:self.byView];
        [self.byView release];
        
        self.byLabel = [[UILabel alloc] init];
        [self.byView addSubview:self.byLabel];
        [self.byLabel release];
        
        self.nameLabel = [[UILabel alloc] init];
        [self.byView addSubview:self.nameLabel];
        [self.nameLabel release];
        
        self.mView = [[UIView alloc] init];
        [self.twoView addSubview:self.mView];
        [self.mView release];
        
        self.sizeLabel = [[UILabel alloc] init];
        [self.mView addSubview:self.sizeLabel];
        [self.sizeLabel release];
        
        self.mLabel = [[UILabel alloc] init];
        [self.mView addSubview:self.mLabel];
        [self.mLabel release];
        
        self.bigImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.bigImageView];
        [self.bigImageView release];
        
    }
    return self;
}

- (void)setTracks:(AlbumTracks *)tracks
{
    if (_tracks != tracks) {
        [_tracks release];
        _tracks = [tracks retain];
    }
    // 标题
    self.titleLabel.text = self.tracks.title;
    // 编者名字
    self.nameLabel.text = self.tracks.nickname;
    
    float number = [self.tracks.playtimes floatValue] / 10000;
    
    NSString *playtimes = [NSString stringWithFormat:@"%.1f万", number];
    
    self.playLabel.text = playtimes;
    
    self.heartLabel.text = [self.tracks.likes description];
    
    // 给"时长"赋值
    CGFloat times = [self.tracks.duration floatValue];
    self.buildTimeLabel.text = [self convertTime:times];
    
    NSURL *url = [NSURL URLWithString:self.tracks.coverSmall];
    [self.bigImageView setImageWithURL:url];
}

- (void)setMusic:(MusicSearch *)music
{
    if (_music != music) {
        [_music release];
        _music = [music retain];
    }
    // 标题
    self.titleLabel.text = self.music.title;
    // 编者名字
    self.nameLabel.text = self.music.nickname;
    
    // 给"播放量"赋值
    float play = [self.music.count_play floatValue];
    if (play < 10000) {
        self.playLabel.text = [NSString stringWithFormat:@"%.0f", play];
    } else if (play > 10000) {
        self.playLabel.text = [NSString stringWithFormat:@"%.1f万", [self.music.count_play floatValue] / 10000];
    }
    
    self.heartLabel.text = [self.music.count_like description];
    
    // 给"时长"赋值
    CGFloat times = [self.music.duration floatValue];
    self.buildTimeLabel.text = [self convertTime:times];
    
    NSURL *url = [NSURL URLWithString:self.music.cover_path];
    [self.bigImageView setImageWithURL:url];
}

// 计算时长的方法
- (NSString *)convertTime:(CGFloat)second{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (second / 3600 >= 1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    } else {
        [formatter setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [formatter stringFromDate:d];
    [formatter release];
    return showtimeNew;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(kCellWidth / 10, self.contentView.bounds.size.height / 10, kCellWidth / 30 * 17, 0);
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.titleLabel sizeToFit];
    
    self.twoView.frame = CGRectMake(kCellWidth / 10, self.contentView.bounds.size.height / 40 * 25, kCellWidth / 30 * 17, 15);
    // By
    self.byView.frame = CGRectMake(0, 0, self.twoView.frame.size.width / 9 * 7, 15);
    
    self.byLabel.frame = CGRectMake(0, 0, self.byView.frame.size.width / 8 * 1, 15);
    self.byLabel.text = @"by";
    self.byLabel.font = [UIFont systemFontOfSize:12];
    self.byLabel.textColor = [UIColor grayColor];
    
    self.nameLabel.frame = CGRectMake(self.byView.frame.size.width / 8 * 1, 0, self.byView.frame.size.width / 8 * 7, 15);
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.textColor = [UIColor grayColor];
    
    // M
    self.mView.frame = CGRectMake(self.twoView.frame.size.width / 9 * 7, 0, self.twoView.frame.size.width / 9 * 2, 15);
    
    self.sizeLabel.frame = CGRectMake(0, 0, self.mView.frame.size.width / 16 * 12, 15);
    //    self.sizeLabel.text = @"4.8";
    self.sizeLabel.font = [UIFont systemFontOfSize:12];
    self.sizeLabel.textColor = [UIColor grayColor];
    self.sizeLabel.textAlignment = NSTextAlignmentRight;
    
    self.mLabel.frame = CGRectMake(self.mView.frame.size.width / 16 * 12, 0, self.mView.frame.size.width / 16 * 4, 15);
    self.mLabel.textColor = [UIColor grayColor];
    self.mLabel.font = [UIFont systemFontOfSize:12];
    
    // 五个小部件 (已自适应)
    self.fiveView.frame = CGRectMake(kCellWidth / 10, self.twoView.frame.origin.y + 25, kCellWidth / 10 * 7, 15);
    // 播放次数(图标)
    self.playView.frame = CGRectMake(-5, 0, self.fiveView.frame.size.width / 5, 15);
    self.playImageView.frame = CGRectMake(0, 0, self.playView.frame.size.width / 3, 15);
    self.playImageView.image = [UIImage imageNamed:@"iconfont-play.png"];
    // 播放次数(文字)
    self.playLabel.frame = CGRectMake(self.playView.frame.size.width / 3, 0, 5 + self.playView.frame.size.width / 3 * 2, 15);
    self.playLabel.font = [UIFont systemFontOfSize:10];
    self.playLabel.textColor = [UIColor grayColor];
    
    // 喜欢(图标)
    self.heartView.frame = CGRectMake(self.fiveView.frame.size.width / 5, 0, self.fiveView.frame.size.width / 5, 15);
    self.heartImageView.frame = CGRectMake(0, 0, self.heartView.frame.size.width / 3, 15);
    self.heartImageView.image = [UIImage imageNamed:@"iconfont-heart.png"];
    // 喜欢(文字)
    self.heartLabel.frame = CGRectMake(self.heartView.frame.size.width / 3, 0, self.heartView.frame.size.width / 3 * 2, 15);
    self.heartLabel.font = [UIFont systemFontOfSize:10];
    self.heartLabel.textColor = [UIColor grayColor];
    
    // 时间(图标)
    self.buildTimeView.frame = CGRectMake(self.fiveView.frame.size.width / 5 * 2, 0, self.fiveView.frame.size.width / 5, 15);
    self.buildTimeImageView.frame = CGRectMake(0, 0, self.buildTimeView.frame.size.width / 3, 15);
    self.buildTimeImageView.image = [UIImage imageNamed:@"iconfont-shijiantime.png"];
    // 时间(文字)
    self.buildTimeLabel.frame = CGRectMake(self.buildTimeView.frame.size.width / 3, 0, self.fiveView.frame.size.width / 3 * 2, 15);
    self.buildTimeLabel.font = [UIFont systemFontOfSize:10];
    self.buildTimeLabel.textColor = [UIColor grayColor];
    
    self.bigImageView.frame = CGRectMake(kCellWidth / 32 * 22, self.contentView.bounds.size.height / 92 * 9, kCellWidth / 6, kCellWidth / 6);
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
